package com.atguigu.spzx.manager.controller;

import com.atguigu.spyx.model.common.Result;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.sys.SysUser;
import com.atguigu.spyx.model.dto.system.LoginDto;
import com.atguigu.spyx.model.vo.system.LoginVo;
import com.atguigu.spyx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.manager.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author LT
 * @create 2023-10-31-19:41
 */
@RestController
//@CrossOrigin(originPatterns="*",allowedHeaders="*" , allowCredentials = "true" )
@RequestMapping("/admin/system/index")
@Tag(name = "Index模块")
public class IndexController {

    @Resource
    SysUserService userService;

    @Operation(summary = "验证码接口",description = "验证码接口")
    @GetMapping("/validateCodeImg")
    public Result validateCodeImg(){
        ValidateCodeVo validateCodeVo = userService.genValidateCodeImg();
        return Result.ok().data(validateCodeVo);
    }

    @Operation(summary = "登录接口",description = "登录接口")
    @Parameters(value = {
            @Parameter(name = "loginDto",description = "登录参数模型",
            in = ParameterIn.HEADER,required = true)
    })
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
      LoginVo loginVo = userService.login(loginDto);
      return Result.ok().data(loginVo);
    }


    @GetMapping("/userInfo")
    public Result userInfo(@RequestHeader("token")String token){
        SysUser sysUser = userService.getUserInfo(token);
        return Result.ok().data(sysUser);
    }

    @Operation(summary = "注销接口",description = "注销接口")
    @Parameters(value = {
            @Parameter(name = "token",description = "登录令牌",
                    in = ParameterIn.HEADER,required = true)
    })
    @GetMapping("/logout")
    public Result Logout(@RequestHeader("token")String token){
        userService.Logout(token);
        return Result.ok();
    }
}
